// 
// Decompiled by Procyon v0.5.36
// 

package com.orange.boot.dev3.service.impl;

import com.orange.boot.dev3.entity.vo.PictureVo;
import java.util.Iterator;
import java.util.List;
import com.orange.boot.dev3.entity.vo.pictureListVo;
import java.util.ArrayList;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.orange.boot.utils.IpAndPortUtil;
import java.io.IOException;
import java.io.File;
import com.orange.boot.utils.R;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.orange.boot.dev3.service.PictureService;
import com.orange.boot.dev3.entity.Picture;
import com.orange.boot.dev3.mapper.PictureMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture> implements PictureService {
    @Value("${uploadPath}")
    String uploadPath;
    @Autowired
    private PictureService pictureService;

    public PictureServiceImpl() {
    }

    public R addOrangePic(MultipartFile pic) {
        if (pic != null) {
            String fileName = System.currentTimeMillis() + pic.getOriginalFilename();
            File dir = new File(this.uploadPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            File targetFile = new File(this.uploadPath, fileName);

            try {
                pic.transferTo(targetFile);
            } catch (IOException var7) {
                var7.printStackTrace();
                return R.error().message("上传失败");
            }

            String ipAndPort = IpAndPortUtil.getIpAndPort();
            String picUrl = "http://" + ipAndPort + "/news/" + fileName;
            return R.ok().data("picUrl", picUrl);
        } else {
            return R.error().message("上传的文件为空！");
        }
    }

    public R getpicAndName(String diseaseId, String citrusPartId) {
        QueryWrapper<Picture> queryWrapper = new QueryWrapper();
        queryWrapper.eq("disease_id", diseaseId);
        queryWrapper.eq("citrus_part_id", citrusPartId);
        List<Picture> pictureList = this.pictureService.list(queryWrapper);
        ArrayList<pictureListVo> list = new ArrayList();
        Iterator var6 = pictureList.iterator();

        while(var6.hasNext()) {
            Picture picture = (Picture)var6.next();
            pictureListVo listVo = new pictureListVo();
            listVo.setPictureUrl(picture.getPictureUrl());
            listVo.setPictureName(picture.getPictureName());
            list.add(listVo);
        }

        return R.ok().data("list", list);
    }

    public R getpic(String diseaseId) {
        QueryWrapper<Picture> queryWrapper = new QueryWrapper();
        queryWrapper.eq("disease_id", diseaseId);
        List<Picture> pictureList = this.pictureService.list(queryWrapper);
        int size = pictureList.size();
        PictureVo[] pictureVos = new PictureVo[size];

        for(int i = 0; i < size; ++i) {
            Picture picture = (Picture)pictureList.get(i);
            PictureVo pictureVo = new PictureVo();
            pictureVo.setPicUrls(picture.getPictureUrl());
            pictureVo.setPicName(picture.getPictureName());
            pictureVos[i] = pictureVo;
        }

        return R.ok().data("pictureVos", pictureVos);
    }

    public R getAllpic(String pictureName) {
        QueryWrapper<Picture> queryWrapper = new QueryWrapper();
        queryWrapper.eq("picture_name", pictureName);
        List<Picture> pictureList = this.pictureService.list(queryWrapper);
        ArrayList<String> List = new ArrayList();
        Iterator var5 = pictureList.iterator();

        while(var5.hasNext()) {
            Picture picture = (Picture)var5.next();
            List.add(picture.getPictureUrl());
        }

        return R.ok().data("List", List);
    }
}

